﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Traders.Controllers;
using Traders.Models;

namespace Traders.Views
{
    public partial class BalanceView : UserControl, TradersView
    {
        public BalanceView()
        {
            InitializeComponent();
        }

        public DataTable DataTable
        {
            get { return (DataTable)this.accountsDataGridView.DataSource; }
            set { this.accountsDataGridView.DataSource = value; }
        }

        public event EventHandler<TradersEventArgs> EventHandler;

        public void FireEvent(TradersEventArgs e)
        {
            if (EventHandler != null)
                EventHandler(this, e);
        }

        #region Obsługa eventów z zewnątrz widoku

        public void HandleEvent(object sender, TradersEventArgs e)
        {
            switch (e.Action)
            {
                case "AccountAdded":
                    ClearAccountInput();
                    break;
                case "ConnectionError":
                    ShowErrorAndExit();
                    break;
            }
        }

        private void ClearAccountInput()
        {
            textLogin.Text = "";
            textPass.Text = "";
            textServer.Text = "";
        }

        private void ShowErrorAndExit()
        {
            MessageBox.Show("Błąd przy połączeniu", "Błąd SUKO", MessageBoxButtons.OK, MessageBoxIcon.Error);
            Application.Exit();
        }

        #endregion

        #region Eventy z widoku

        private void addAccount_Click(object sender, EventArgs e)
        {
            TradersEventArgs args = new TradersEventArgs();
            args.Action = "AddAccountClicked";
            args.Tag = new Account(textLogin.Text, textPass.Text, textServer.Text);
            FireEvent(args);
        }

        #endregion
    }

}
